package com.longrise.co.ui.carfiles.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.ui.carfiles.adapter.RepairRecordAdapter;
import com.longrise.co.ui.carfiles.contract.CarFilesContract;
import com.longrise.co.ui.carfiles.model.CarFilesModel;
import com.longrise.co.ui.carfiles.model.RepairRecord;
import com.longrise.co.ui.carfiles.model.RxRepairRecordBean;
import com.longrise.co.ui.carfiles.presenter.CarFilesPresenter;
import com.longrise.co.ui.home.model.BindCarInfo;
import com.longrise.co.utils.Constants;
import com.longrise.co.utils.StringUtils;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.GlideCircleTransfromUtil;

import butterknife.Bind;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by luoyang on 2017-7-16.
 */

public class RepairRecordFragment extends BaseFragment<CarFilesPresenter, CarFilesModel> implements CarFilesContract.View<RepairRecord>, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnRecyclerViewItemClickListener {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.car_no)
    TextView carNo;
    @Bind(R.id.car_type)
    TextView carType;
    @Bind(R.id.repair_value)
    TextView repairValue;
    @Bind(R.id.money_value)
    TextView moneyValue;
    @Bind(R.id.car_photo)
    ImageView carPhoto;
    @Bind(R.id.search_iv)
    ImageView searchIv;
    @Bind(R.id.search_no_data_tv)
    TextView searchNoDataTv;
    @Bind(R.id.no_data_tip)
    RelativeLayout noDataTip;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private BindCarInfo.ListBean mCarInfo;
    private RepairRecordAdapter mAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.fg_repair_record;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        mCarInfo = (BindCarInfo.ListBean)getArguments().getSerializable("carInfo");
        noDataTip.setVisibility(View.GONE);
        searchNoDataTv.setText("未查询到该车辆维修记录");
        setText(headTitle, "维修记录");
        initRecyclerView();
        initHead();
        noDataTip.setVisibility(View.GONE);
        mPresenter.getRepairRecordList(mCarInfo.getCarno(), mCarInfo.getVin(),"", true);
    }

    private void initHead() {
        if(mCarInfo == null) {
            return ;
        }
        setText(carNo, mCarInfo.getCarno());
        setText(carType, mCarInfo.getCartype());
        setText(repairValue, String.valueOf(mCarInfo.getRepairnum()));
//        setText(moneyValue, "0");

        if(!StringUtils.isEmpty(mCarInfo.getCarphoto())) {
            Glide.with(getContext()).load(mCarInfo.getCarphoto())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.ic_image_loading)
                    .error(R.drawable.car)
                    .centerCrop().transform(new GlideCircleTransfromUtil(getContext())).into(carPhoto);
        } else {
            Glide.with(getContext()).load(R.drawable.car)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.ic_image_loading)
                    .error(R.drawable.car)
                    .centerCrop().transform(new GlideCircleTransfromUtil(getContext())).into(carPhoto);
        }


        mRxManager.on(Constants.RX_EVENT_REPAIR_RECORD, new Action1<RxRepairRecordBean>() {
            @Override
            public void call(RxRepairRecordBean bean) {
                if("1".equals(bean.type)) {
                    //投诉
                    start(RepairComplaintFragment.newInstance(bean.companyId, bean.repairId, bean.years, null));
                } else if("2".equals(bean.type)) {
                    //评价
                    start(RepairEvaluationFragment.newInstance(bean.companyId, bean.repairId, bean.years));
                } else if("3".equals(bean.type)) {
                    //维修详情
                    start(RepairDetailFragment.newInstance(bean.repairId, bean.years, bean.carNo, bean.isComplaint, bean.isEvaluation));
                } else if("4".equals(bean.type)) {
                    //刷新维修记录
                    onRefresh();
                }
            }
        });
    }

    private void initRecyclerView() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeColors(Constants.RECYCLER_RGB);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new RepairRecordAdapter(null);

        mAdapter.openLoadMore(ApiConstants.PAGE_SIZE_10, false);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        recyclerView.setAdapter(mAdapter);
    }



    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    public static RepairRecordFragment newInstance(BindCarInfo.ListBean carInfo) {
        
        Bundle args = new Bundle();
        args.putSerializable("carInfo", carInfo);
        RepairRecordFragment fragment = new RepairRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onRefresh() {
        noDataTip.setVisibility(View.GONE);
        mPresenter.getRepairRecordList(mCarInfo.getCarno(), mCarInfo.getVin(),"", false);
    }


    @Override
    public void onItemClick(View view, int i) {
        if(i >= 0 && i < mAdapter.getData().size()) {
            RepairRecord.ListBean bean = mAdapter.getData().get(i);
            boolean isEvaluation = "0".equals(bean.getEvaluateflag()) ? false : true;
            boolean isComplaint = "1".equals(bean.getComplaintflag()) ? false : true;
            start(RepairDetailFragment.newInstance(bean.getRecordid(), bean.getYears(), bean.getCarno(),isComplaint, isEvaluation));
        }
    }

    @Override
    public void requestSuccess(RepairRecord info) {
        if (info != null && info.getList() != null && info.getList().size() > 0) {
            mAdapter.setNewData(info.getList());
        } else {
            //没有搜索到结果
            noDataTip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onComplete() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }
}
